﻿using B.S.BasicData.Api.Read.Application.Command.Approval;
using B.S.BasicData.Api.Read.Application.Handler.Approval;
using B.S.BasicData.Api.Read.DTOS;
using B.S.BasicData.Domain.Entity.Approval;
using B.S.BasicData.Domian.Entitys;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infastructure.Interface;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.Collections.Generic;
using System.Net.Mime;
using static System.Net.Mime.MediaTypeNames;

namespace B.S.BasicData.Api.Read.Controllers
{
    /// <summary>
    /// 请假服务
    /// </summary>
    [Route("api/[controller][action]")]
    [ApiController]
    [Authorize]
    public class ApprovalController : ControllerBase
    {
        IMediator _mediator;
        private readonly IBaseRepository<UsersModel> _userRepository;
        private readonly IBaseRepository<ApprovalModel> _approvalRepository;

        public ApprovalController(IMediator mediator, IBaseRepository<UsersModel> userRepository, IBaseRepository<ApprovalModel> approvalRepository)
        {
            _mediator = mediator;
            _userRepository = userRepository;
            _approvalRepository = approvalRepository;
        }
        /// <summary>
        /// 根据用户获取部门信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public Task<ApiResult<List<DeparmentDto>>> GetDeparment([FromQuery]GetDeparmentCommand request)
        {
            return _mediator.Send(request);
        }
        /// <summary>
        /// 获取请假类型
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public Task<ApiResult<List<LeaveTypeDto>>> GetLeave([FromQuery]GetLeaveTypeCommand request)
        {
            return _mediator.Send(request);
        }
        /// <summary>
        /// 获取审批列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiPaging<ApprovalListDto>> GetApprovalList([FromQuery]GetApprovalListCommand request)
        {
            return _mediator.Send(request);
        }
        /// <summary>
        /// 获取报表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public Task<ApiResult<List<ReportDto>>> GetReport([FromQuery]GetReportlistCommand request)
        {
            return _mediator.Send(request);
        }
        /// <summary>
        /// 获取工时报表详情
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiPaging<ReportDetailDto>> GetReportDetail([FromQuery]GetReportDetailCommand request)
        {
            return _mediator.Send(request);
        }
        /// <summary>
        /// 导出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ExportExcel()
        {
            var user = _userRepository.GetAll();
            var approval = _approvalRepository.GetAll();

            var report = from u in user
                         join a in approval on u.Id equals a.UserId
                         select new { u.UserName, a.StartTime, a.Hours, a.YeNo };

            var list = from a in report.Where(x => x.YeNo == false)
                       group a by a.UserName into form
                       select new ReportDto()
                       {
                           UserName = form.Key,
                           Month1 = form.Where(x => x.StartTime.Month == 1 && x.StartTime.Year == 2025).Sum(x => x.Hours),
                           Month2 = form.Where(x => x.StartTime.Month == 2 && x.StartTime.Year == 2025).Sum(x => x.Hours),
                           Month3 = form.Where(x => x.StartTime.Month == 3 && x.StartTime.Year == 2025).Sum(x => x.Hours),
                           Month4 = form.Where(x => x.StartTime.Month == 4 && x.StartTime.Year == 2025).Sum(x => x.Hours),
                           Month5 = form.Where(x => x.StartTime.Month == 5 && x.StartTime.Year == 2025).Sum(x => x.Hours),
                           Month6 = form.Where(x => x.StartTime.Month == 6 && x.StartTime.Year == 2025).Sum(x => x.Hours),
                           Month7 = form.Where(x => x.StartTime.Month == 7 && x.StartTime.Year == 2025).Sum(x => x.Hours),
                           Month8 = form.Where(x => x.StartTime.Month == 8 && x.StartTime.Year == 2025).Sum(x => x.Hours),
                           Month9 = form.Where(x => x.StartTime.Month == 9 && x.StartTime.Year == 2025).Sum(x => x.Hours),
                           Month10 = form.Where(x => x.StartTime.Month == 10 && x.StartTime.Year == 2025).Sum(x => x.Hours),
                           Month11 = form.Where(x => x.StartTime.Month == 11 && x.StartTime.Year == 2025).Sum(x => x.Hours),
                           Month12 = form.Where(x => x.StartTime.Month == 12 && x.StartTime.Year == 2025).Sum(x => x.Hours),
                       };

            XSSFWorkbook wb = new XSSFWorkbook();
            ISheet sheet = wb.CreateSheet("请假工时报表");

            //创建行
            IRow row = sheet.CreateRow(0);
            row.CreateCell(0).SetCellValue("用户/请假月份");
            row.CreateCell(1).SetCellValue("一月");
            row.CreateCell(2).SetCellValue("二月");
            row.CreateCell(3).SetCellValue("三月");
            row.CreateCell(4).SetCellValue("四月");
            row.CreateCell(5).SetCellValue("五月");
            row.CreateCell(6).SetCellValue("六月");
            row.CreateCell(7).SetCellValue("七月");
            row.CreateCell(8).SetCellValue("八月");
            row.CreateCell(9).SetCellValue("九月");
            row.CreateCell(10).SetCellValue("十月");
            row.CreateCell(11).SetCellValue("十一月");
            row.CreateCell(12).SetCellValue("十二月");

            int index = 1;
            foreach(var item in list)
            {
                IRow row1 = sheet.CreateRow(index);
                row1.CreateCell(0).SetCellValue(item.UserName);
                row1.CreateCell(1).SetCellValue(item.Month1.ToString());
                row1.CreateCell(2).SetCellValue(item.Month2.ToString());
                row1.CreateCell(3).SetCellValue(item.Month3.ToString());
                row1.CreateCell(4).SetCellValue(item.Month4.ToString());
                row1.CreateCell(5).SetCellValue(item.Month5.ToString());
                row1.CreateCell(6).SetCellValue(item.Month6.ToString());
                row1.CreateCell(7).SetCellValue(item.Month7.ToString());
                row1.CreateCell(8).SetCellValue(item.Month8.ToString());
                row1.CreateCell(9).SetCellValue(item.Month9.ToString());
                row1.CreateCell(10).SetCellValue(item.Month10.ToString());
                row1.CreateCell(11).SetCellValue(item.Month11.ToString());
                row1.CreateCell(12).SetCellValue(item.Month12.ToString());
                index += 1;
            }
            //把创建对象转换成byte[]
            byte[] data = null;
            using(MemoryStream ms = new MemoryStream())
            {
                wb.Write(ms);
                data = ms.ToArray();
            }
            return File(data,"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet","请假工时报表.xlsx");
        }

    }
}
